<?php
/**
 * 
 * @authors Liujinbi (857053791@QQ.com)
 * @date    2017-07-21 19:47:57
 * @version $Id$
 */
namespace app\admin\controller;
use think\Request;
use think\Db;
use think\Controller;
use Godok\Org\Auth;
use think\Config;

class Shopuser extends Base {
    public function index(){
		$keywords = request() -> param('keywords');
		$sort = Request()->get('register_time')?Request()->get('register_time'):"register_time desc";
		$this->assign('sort',$sort);
		if($keywords){
			$user = db('user') -> where('nickname|username|phone','like','%'.$keywords.'%')->where('status!=2')->order($sort);
		}else{
			$user = db() -> table('sdzx_user') -> alias('u')
			->field('u.*,s.*')
			->join('sdzx_shop s','u.shop_id=s.shop_id','left')
			->where('u.status!=2')
			->order($sort);
		}
		
		$user = $user -> paginate('',false,['query'=>Request()->param()]);

		$this->assign('user',$user);
		return $this->fetch();
	}
	
	public function details(){
		$user_id = input('user_id');
		
		$user = db()->table('sdzx_user')->alias('u')
		->field('u.*,s.*')
		->join('sdzx_shop s','u.shop_id=s.shop_id')
		->where('u.id',$user_id)
		->find();
		
		$order = db('order')->where('user_id',$user_id)->select();
		$invoice = db('invoice')->where('user_id',$user_id)->find();
		
		$this->assign('invoice',$invoice);
		$this->assign('order',$order);
		$this->assign('info',$user);
		return $this->fetch();
    }
	
	public function edit(){
		$user_id = input('user_id');
		
		$data = [
			'nickname' => input('nickname'),
			'realname' => input('realname'),
			'username' => input('username'),
		];
		
		$map = [
			'shop_name' => input('shop_name'),
			'shop_owner' => input('shop_owner'),
			'shop_address' => input('shop_address'),
		];
		
		$res1 = db('user')->where('id',$user_id)->update($data);
		$res2 = db('shop')->where('user_id',$user_id)->update($map);
		
		if($res1!==false && $res2!==false){
			$this->success('编辑成功',url('index'));
		}else{
			$this->error('编辑失败');
		}
	}

	//分配管家
	public function allot()
    {
    	if (Request()->isPost()) {
    		$param = Request()->param();
    		//员工信息
    		$employee = DB::name('employee')->where(['id'=>$param['uid'],'deleted'=>0,'status'=>1])->find();
    		if (!$employee) {
    			$this->error('配送员不存在或已禁用');
    		}

    		$re = Db::name('shop')->where(['shop_id'=>$param['ids']])->update(['agent_id'=>$employee['id']]);
    		if ($re!==false) {
    			$this->success('操作成功','refresh');
    		}else{
    			$this->error('操作失败');
    		}
    	}else{
	    	$keywords = Request()->get('keywords');
			// 配送人员
			$Query = Db::field('e.id,e.nickname,e.name,e.phone,g.code,count(s.agent_id) as num')
			->table('sdzx_employee')->alias('e')
			->join('sdzx_employee_group_relation r','e.id=r.uid')
			->join('sdzx_employee_group g','r.group_id=g.id')
			->join('sdzx_shop s','e.id=s.agent_id','left')
			->where("g.code='GJ' and e.status=1 and e.deleted=0")
			->group('e.id');

			if ($keywords) {
				$Query->where("e.name like '%{$keywords}%' or e.phone like '%{$keywords}%'");
			}
			$list = $Query->select();
			return $this->fetch('allot',['list'=>$list]);
		}
    }
}